﻿using SF.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace SF.Ucenter.admin.manager
{
    public partial class notice_edit : Web.UI.ManagePage
    {
        private string action = DTEnums.ActionEnum.Add.ToString(); //操作类型
        private int id = 0;

        //页面加载事件
        protected void Page_Load(object sender, EventArgs e)
        {
            string _action = DTRequest.GetQueryString("action");

            if (!string.IsNullOrEmpty(_action) && _action == DTEnums.ActionEnum.Edit.ToString())
            {
                this.action = DTEnums.ActionEnum.Edit.ToString();//修改类型
                this.id = DTRequest.GetQueryInt("id");
                if (this.id == 0)
                {
                    JscriptMsg("传输参数不正确！", "back", "Error");
                    return;
                }
                if (!new BLL.sf_notice().Exists(this.id))
                {
                    JscriptMsg("通知不存在或已被删除！", "back", "Error");
                    return;
                }
            }
            if (!Page.IsPostBack)
            {
                ChkAdminLevel("sys_notice", DTEnums.ActionEnum.View.ToString()); //检查权限

                if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
                {
                    ShowInfo(this.id);
                }
            }
        }

        #region 赋值操作
        private void ShowInfo(int _id)
        {
            BLL.sf_notice bll = new BLL.sf_notice();
            Model.sf_notice model = bll.GetModel(_id);
            txtTitle.Text = model.titles;
            txtDetail.InnerText = model.details;
        }
        #endregion


        #region 增加操作
        private bool DoAdd()
        {
            Model.manager mananger  = Session[DTKeys.SESSION_ADMIN_INFO] as Model.manager;
            bool result = false;
            Model.sf_notice model = new Model.sf_notice();
            BLL.sf_notice bll = new BLL.sf_notice();
            model.busnieseNum =mananger.busnieseNum;
            model.appNum = Session[DTKeys.SESSION_APP_INFO].ToString();
            model.templatesNum = Session[DTKeys.SESSION_TEMPLATES_INFO].ToString();
            model.userNum = mananger != null ? mananger.real_name : "无";
            model.addTime = DateTime.Now;
            model.titles = txtTitle.Text;
            model.details = txtDetail.Value;
            if (bll.Add(model) > 0)
            {
                AddAdminLog(DTEnums.ActionEnum.Add.ToString(), "添加通知内容:" + model.titles); //记录日志
                result = true;
            }
            return result;
        }
        #endregion

        #region 修改操作
        private bool DoEdit(int _id)
        {
            bool result = false;
            BLL.sf_notice bll = new BLL.sf_notice();
            Model.sf_notice model = bll.GetModel(_id);

            model.titles = txtTitle.Text;
            model.details = txtDetail.Value;
            if (bll.Update(model))
            {
                AddAdminLog(DTEnums.ActionEnum.Edit.ToString(), "修改通知内容:" + model.titles); //记录日志
                result = true;
            }
            return result;
        }
        #endregion


        //保存
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            if (action == DTEnums.ActionEnum.Edit.ToString()) //修改
            {
                ChkAdminLevel("sys_notice", DTEnums.ActionEnum.Edit.ToString()); //检查权限
                if (!DoEdit(this.id))
                {
                    JscriptMsg("保存过程中发生错误啦！", "");
                    return;
                }
                JscriptMsg("修改信息成功！", "notice_list.aspx");
            }
            else //添加
            {
                ChkAdminLevel("sys_notice", DTEnums.ActionEnum.Add.ToString()); //检查权限
                if (!DoAdd())
                {
                    JscriptMsg("保存过程中发生错误啦！", "");
                    return;
                }
                JscriptMsg("添加信息成功！", "notice_list.aspx");
            }
        }
    }
}